package com.xysd.express.activity.mine.account;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.xysd.express.R;
import com.xysd.express.adapter.AccountDetailsAdpter;
import com.xysd.express.base.BaseActivity;
import com.xysd.express.bean.AccountDetailsObj;
import com.xysd.express.util.JSONParser;
import com.xysd.express.util.ToastUtil;
import com.xysd.express.util.http.HttpCallBack;
import com.xysd.express.util.http.HttpUtil;
import com.xysd.express.widget.BaseSwipeRefreshLayout;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

@ContentView(R.layout.activity_account_details)
public class AccountDetailsActivity extends BaseActivity {

    @ViewInject(R.id.account_rv)
    private RecyclerView account_rv;
    @ViewInject(R.id.swiperefreshlayout)
    private BaseSwipeRefreshLayout swiperefreshlayout;

    AccountDetailsAdpter adapter;
    List<AccountDetailsObj.AccountDetails>dataList;
    List<AccountDetailsObj.AccountDetails>allDataList=new ArrayList<>();

    int page=1;
    int pageSize=10;
    int totleSize;
    @Override
    protected void init() {
        setTitleBar("资金明细");
        dataList=new ArrayList<>();
        adapter=new AccountDetailsAdpter(R.layout.item_account_details,mActivity);
        account_rv.setLayoutManager(new LinearLayoutManager(mActivity));
        account_rv.setAdapter(adapter);
        adapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                page++;
                getData();
            }
        },account_rv);

        getData();
        initRefresh();
    }

    private void initRefresh() {
        setSwipeRefreshLayout(swiperefreshlayout);
        setOnRefreshListener(new BaseSwipeRefreshLayout.OnRefresh() {
            @Override
            public void refresh() {
                page=1;
                allDataList.clear();
                getData();
            }
        });
    }

    private void getData() {
        HttpUtil.getAccountDetails(page,pageSize,new HttpCallBack() {
            @Override
            public void onSuccess(String result) {
                Log.i("账户明细",result);
                String code= JSONParser.getStringFromJsonString("Status",result);
                String msg= JSONParser.getStringFromJsonString("Msg",result);
                if ("0".equals(code)){
                    dataList =new Gson().fromJson(result,AccountDetailsObj.class).Data;
                    totleSize =new Gson().fromJson(result,AccountDetailsObj.class).Total;
                    if(null==dataList||dataList.size()==0){
                        adapter.loadMoreEnd();
                        return;
                    }
                    if (page==1){
                        adapter.setNewData(dataList);
                        allDataList=dataList;
                    }else {
                        adapter.addData(dataList);
                        allDataList.addAll(dataList);//加载下一页数据之后添加到总数据里面，以供adapter的onItemClickListener使用
                    }
                    //判断是否已经获取完所有数据
                    if (dataList.size()<pageSize){
                        adapter.loadMoreEnd();
                    }else {
                        if (page*pageSize==totleSize){
                            adapter.loadMoreEnd();
                        }else {
                            adapter.loadMoreComplete();
                        }
                    }
                }
                else {
                    ToastUtil.show(msg);
                }
            }

            @Override
            public void onFailure(String error) {
                Log.e("账户明细",error);
            }
        });
    }
}
